<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
   
  <meta name="keywords" content="Java Python" />
   
  <meta name="description" content="From Zero to Hero" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Redis-06-Jedis |  朱酱酱的学习博客
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="朱酱酱的学习博客" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Redis/Redis-06-JRedis" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Redis-06-Jedis
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/04/12/Redis/Redis-06-JRedis/" class="article-date">
  <time datetime="2020-04-12T11:02:24.000Z" itemprop="datePublished">2020-04-12</time>
</a>
      
      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">3.4k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">16分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="Redis-06-Jedis"><a href="#Redis-06-Jedis" class="headerlink" title="Redis-06-Jedis"></a>Redis-06-Jedis</h1><h2 id="1-Jedis概述"><a href="#1-Jedis概述" class="headerlink" title="1. Jedis概述"></a>1. Jedis概述</h2><p>Jedis是Redis官网推荐的java连接开发工具！ </p>
<p>使用java操作Redis的中间件</p>
<p>如果要使用java操作redis，那么一定要对Jedis 十分的熟悉！</p>
<a id="more"></a>



<h2 id="2-测试"><a href="#2-测试" class="headerlink" title="2. 测试"></a>2. 测试</h2><ol>
<li>导入对应的依赖</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>fastjson<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.68<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- https://mvnrepository.com/artifact/redis.clients/jedis --&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>redis.clients<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jedis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.7.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>



<ol start="2">
<li>编码测试</li>
</ol>
<ul>
<li>连接数据库</li>
<li>操作命令</li>
<li>断开链接</li>
</ul>
<h3 id="2-1-连接数据库"><a href="#2-1-连接数据库" class="headerlink" title="2.1 连接数据库"></a>2.1 连接数据库</h3><ol>
<li>设置redis密码</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 找到redis.conf 并修改 daemonize no 为 daemonize yes ，这样就可以默认启动就后台运行</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 切换到redis根目录，先启动服务端</span></span><br><span class="line">redis-server 【配置文件】</span><br><span class="line"></span><br><span class="line"><span class="comment">## 切换到redis根目录，启动客户端</span></span><br><span class="line">&gt;redis-cli -h 127.0.0.1 -p 6379</span><br><span class="line"></span><br><span class="line"><span class="comment">## 客户端使用config get requirepass命令查看密码</span></span><br><span class="line">&gt;config get requirepass</span><br><span class="line">1)<span class="string">"requirepass"</span></span><br><span class="line">2)<span class="string">""</span>    //默认空</span><br><span class="line"></span><br><span class="line"><span class="comment">## 客户端设置密码</span></span><br><span class="line">&gt;config <span class="built_in">set</span> requirepass 123456</span><br><span class="line">&gt;OK</span><br><span class="line"></span><br><span class="line"><span class="comment">## 一旦设置密码，必须先通过密码验证，否则所有操作不可用</span></span><br><span class="line">&gt;config get requirepass</span><br><span class="line">(error)NOAUTH Authentication required</span><br><span class="line"></span><br><span class="line"><span class="comment">## 使用auth password验证密码</span></span><br><span class="line">&gt;auth 123456</span><br><span class="line">&gt;OK</span><br><span class="line">&gt;config get requirepass</span><br><span class="line">1)<span class="string">"requirepass"</span></span><br><span class="line">2)<span class="string">"123456"</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>修改远程配置文件设置</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 注释掉bind 127.0.0.1</span></span><br><span class="line"><span class="comment">## protected-mode yes 修改为 no</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li>测试连接</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zhuuu;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestPing</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1. new Jedis对象</span></span><br><span class="line">        Jedis jedis = <span class="keyword">new</span> Jedis(<span class="string">"服务器ip地址"</span>,<span class="number">6379</span>);</span><br><span class="line">        jedis.auth(<span class="string">"123456"</span>);</span><br><span class="line">        <span class="comment">// 2. jedis所有的命令就是我们之前学习的所有指令！</span></span><br><span class="line">        System.out.println(jedis.ping());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="2-2-常用的API"><a href="#2-2-常用的API" class="headerlink" title="2.2 常用的API"></a>2.2 常用的API</h3><p><strong>五大类型</strong></p>
<p>String</p>
<p>List</p>
<p>Set</p>
<p>Hash</p>
<p>Zset</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//连接redis ，redis的默认端口是6379</span></span><br><span class="line"></span><br><span class="line">Jedis  jedis = <span class="keyword">new</span> Jedis (<span class="string">"localhost"</span>,<span class="number">6379</span>); </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//验证密码，如果没有设置密码这段代码省略</span></span><br><span class="line"></span><br><span class="line">jedis.auth(<span class="string">"password"</span>); </span><br><span class="line"></span><br><span class="line">jedis.connect();<span class="comment">//连接</span></span><br><span class="line"></span><br><span class="line">jedis.disconnect();<span class="comment">//断开连接</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Set&lt;String&gt; keys = jedis.keys(<span class="string">"*"</span>); <span class="comment">//列出所有的key</span></span><br><span class="line"></span><br><span class="line">Set&lt;String&gt; keys = jedis.keys(<span class="string">"key"</span>); <span class="comment">//查找特定的key</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="comment">//移除给定的一个或多个key,如果key不存在,则忽略该命令. </span></span><br><span class="line"></span><br><span class="line">jedis.del(<span class="string">"key1"</span>);</span><br><span class="line"></span><br><span class="line">jedis.del(<span class="string">"key1"</span>,<span class="string">"key2"</span>,<span class="string">"key3"</span>,<span class="string">"key4"</span>,<span class="string">"key5"</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//移除给定key的生存时间(设置这个key永不过期)</span></span><br><span class="line">jedis.persist(<span class="string">"key1"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//检查给定key是否存在</span></span><br><span class="line">jedis.exists(<span class="string">"key1"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//将key改名为newkey,当key和newkey相同或者key不存在时,返回一个错误</span></span><br><span class="line">jedis.rename(<span class="string">"key1"</span>, <span class="string">"key2"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//返回key所储存的值的类型。 </span></span><br><span class="line"><span class="comment">//none(key不存在),string(字符串),list(列表),set(集合),zset(有序集),hash(哈希表) </span></span><br><span class="line">jedis.type(<span class="string">"key1"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置key生存时间，当key过期时，它会被自动删除。 </span></span><br><span class="line">jedis.expire(<span class="string">"key1"</span>, <span class="number">5</span>);<span class="comment">//5秒过期 </span></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//字符串值value关联到key。 </span></span><br><span class="line">jedis.set(<span class="string">"key1"</span>, <span class="string">"value1"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//将值value关联到key，并将key的生存时间设为seconds(秒)。 </span></span><br><span class="line">jedis.setex(<span class="string">"foo"</span>, <span class="number">5</span>, <span class="string">"haha"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//清空所有的key</span></span><br><span class="line">jedis.flushAll();</span><br><span class="line"></span><br><span class="line"><span class="comment">//返回key的个数 </span></span><br><span class="line">jedis.dbSize();</span><br><span class="line"></span><br><span class="line"><span class="comment">//哈希表key中的域field的值设为value。 </span></span><br><span class="line">jedis.hset(<span class="string">"key1"</span>, <span class="string">"field1"</span>, <span class="string">"field1-value"</span>); </span><br><span class="line">jedis.hset(<span class="string">"key1"</span>, <span class="string">"field2"</span>, <span class="string">"field2-value"</span>); </span><br><span class="line"></span><br><span class="line">Map map = <span class="keyword">new</span> HashMap(); </span><br><span class="line">map.put(<span class="string">"field1"</span>, <span class="string">"field1-value"</span>); </span><br><span class="line">map.put(<span class="string">"field2"</span>, <span class="string">"field2-value"</span>); </span><br><span class="line">jedis.hmset(<span class="string">"key1"</span>, map); </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//返回哈希表key中给定域field的值 </span></span><br><span class="line">jedis.hget(<span class="string">"key1"</span>, <span class="string">"field1"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//返回哈希表key中给定域field的值(多个)</span></span><br><span class="line">List list = jedis.hmget(<span class="string">"key1"</span>,<span class="string">"field1"</span>,<span class="string">"field2"</span>); </span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;list.size();i++)&#123; </span><br><span class="line">   System.out.println(list.get(i)); </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//返回哈希表key中所有域和值</span></span><br><span class="line">Map&lt;String,String&gt; map = jedis.hgetAll(<span class="string">"key1"</span>); </span><br><span class="line"><span class="keyword">for</span>(Map.Entry entry: map.entrySet()) &#123; </span><br><span class="line">   System.out.print(entry.getKey() + <span class="string">":"</span> + entry.getValue() + <span class="string">"\t"</span>); </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//删除哈希表key中的一个或多个指定域</span></span><br><span class="line">jedis.hdel(<span class="string">"key1"</span>, <span class="string">"field1"</span>);</span><br><span class="line">jedis.hdel(<span class="string">"key1"</span>, <span class="string">"field1"</span>,<span class="string">"field2"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//查看哈希表key中，给定域field是否存在。 </span></span><br><span class="line">jedis.hexists(<span class="string">"key1"</span>, <span class="string">"field1"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//返回哈希表key中的所有域</span></span><br><span class="line">jedis.hkeys(<span class="string">"key1"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//返回哈希表key中的所有值</span></span><br><span class="line">jedis.hvals(<span class="string">"key1"</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//将值value插入到列表key的表头。 </span></span><br><span class="line">jedis.lpush(<span class="string">"key1"</span>, <span class="string">"value1-0"</span>); </span><br><span class="line">jedis.lpush(<span class="string">"key1"</span>, <span class="string">"value1-1"</span>); </span><br><span class="line">jedis.lpush(<span class="string">"key1"</span>, <span class="string">"value1-2"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//返回列表key中指定区间内的元素,区间以偏移量start和stop指定.</span></span><br><span class="line"><span class="comment">//下标(index)参数start和stop从0开始;</span></span><br><span class="line"><span class="comment">//负数下标代表从后开始(-1表示列表的最后一个元素,-2表示列表的倒数第二个元素,以此类推)</span></span><br><span class="line">List list = jedis.lrange(<span class="string">"key1"</span>, <span class="number">0</span>, -<span class="number">1</span>);<span class="comment">//stop下标也在取值范围内(闭区间)</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;list.size();i++)&#123; </span><br><span class="line">   System.out.println(list.get(i)); </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//返回列表key的长度。 </span></span><br><span class="line">jedis.llen(<span class="string">"key1"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//将member元素加入到集合key当中。 </span></span><br><span class="line">jedis.sadd(<span class="string">"key1"</span>, <span class="string">"value0"</span>); </span><br><span class="line">jedis.sadd(<span class="string">"key1"</span>, <span class="string">"value1"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//移除集合中的member元素。 </span></span><br><span class="line">jedis.srem(<span class="string">"key1"</span>, <span class="string">"value1"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//返回集合key中的所有成员。 </span></span><br><span class="line">Set set = jedis.smembers(<span class="string">"key1"</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">//判断元素是否是集合key的成员</span></span><br><span class="line">jedis.sismember(<span class="string">"key1"</span>, <span class="string">"value2"</span>)); </span><br><span class="line"></span><br><span class="line"><span class="comment">//返回集合key的元素的数量</span></span><br><span class="line">jedis.scard(<span class="string">"key1"</span>);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//返回一个集合的全部成员，该集合是所有给定集合的交集</span></span><br><span class="line">jedis.sinter(<span class="string">"key1"</span>,<span class="string">"key2"</span>)</span><br><span class="line"> </span><br><span class="line"><span class="comment">//返回一个集合的全部成员，该集合是所有给定集合的并集</span></span><br><span class="line">jedis.sunion(<span class="string">"key1"</span>,<span class="string">"key2"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//返回一个集合的全部成员，该集合是所有给定集合的差集</span></span><br><span class="line">jedis.sdiff(<span class="string">"key1"</span>,<span class="string">"key2"</span>);</span><br></pre></td></tr></table></figure>

<ol>
<li><strong>对Key的操作</strong></li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testKey</span><span class="params">()</span></span>&#123;</span><br><span class="line">    System.out.println(<span class="string">"清空数据："</span>+jedis.flushDB());</span><br><span class="line">    System.out.println(<span class="string">"判断某个键是否存在："</span>+jedis.exists(<span class="string">"username"</span>));</span><br><span class="line">    System.out.println(<span class="string">"新增&lt;'username','zzh'&gt;的键值对："</span>+jedis.set(<span class="string">"username"</span>, <span class="string">"zzh"</span>));</span><br><span class="line">    System.out.println(<span class="string">"新增&lt;'password','password'&gt;的键值对："</span>+jedis.set(<span class="string">"password"</span>, <span class="string">"password"</span>));</span><br><span class="line">    System.out.print(<span class="string">"系统中所有的键如下："</span>);</span><br><span class="line">    Set&lt;String&gt; keys = jedis.keys(<span class="string">"*"</span>);</span><br><span class="line">    System.out.println(keys);</span><br><span class="line">    System.out.println(<span class="string">"删除键password:"</span>+jedis.del(<span class="string">"password"</span>));</span><br><span class="line">    System.out.println(<span class="string">"判断键password是否存在："</span>+jedis.exists(<span class="string">"password"</span>));</span><br><span class="line">    System.out.println(<span class="string">"查看键username所存储的值的类型："</span>+jedis.type(<span class="string">"username"</span>));</span><br><span class="line">    System.out.println(<span class="string">"随机返回key空间的一个："</span>+jedis.randomKey());</span><br><span class="line">    System.out.println(<span class="string">"重命名key："</span>+jedis.rename(<span class="string">"username"</span>,<span class="string">"name"</span>));</span><br><span class="line">    System.out.println(<span class="string">"取出改后的name："</span>+jedis.get(<span class="string">"name"</span>));</span><br><span class="line">    System.out.println(<span class="string">"按索引查询："</span>+jedis.select(<span class="number">0</span>));</span><br><span class="line">    System.out.println(<span class="string">"删除当前选择数据库中的所有key："</span>+jedis.flushDB());</span><br><span class="line">    System.out.println(<span class="string">"返回当前数据库中key的数目："</span>+jedis.dbSize());</span><br><span class="line">    System.out.println(<span class="string">"删除所有数据库中的所有key："</span>+jedis.flushAll());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>对String的操作</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testString</span><span class="params">()</span></span>&#123;</span><br><span class="line">    jedis.flushDB();</span><br><span class="line">    System.out.println(<span class="string">"===========增加数据==========="</span>);</span><br><span class="line">    System.out.println(jedis.set(<span class="string">"key1"</span>,<span class="string">"value1"</span>));</span><br><span class="line">    System.out.println(jedis.set(<span class="string">"key2"</span>,<span class="string">"value2"</span>));</span><br><span class="line">    System.out.println(jedis.set(<span class="string">"key3"</span>, <span class="string">"value3"</span>));</span><br><span class="line">    System.out.println(<span class="string">"删除键key2:"</span>+jedis.del(<span class="string">"key2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"获取键key2:"</span>+jedis.get(<span class="string">"key2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"修改key1:"</span>+jedis.set(<span class="string">"key1"</span>, <span class="string">"value1Changed"</span>));</span><br><span class="line">    System.out.println(<span class="string">"获取key1的值："</span>+jedis.get(<span class="string">"key1"</span>));</span><br><span class="line">    System.out.println(<span class="string">"在key3后面加入值："</span>+jedis.append(<span class="string">"key3"</span>, <span class="string">"End"</span>));</span><br><span class="line">    System.out.println(<span class="string">"key3的值："</span>+jedis.get(<span class="string">"key3"</span>));</span><br><span class="line">    System.out.println(<span class="string">"增加多个键值对："</span>+jedis.mset(<span class="string">"key01"</span>,<span class="string">"value01"</span>,<span class="string">"key02"</span>,<span class="string">"value02"</span>,<span class="string">"key03"</span>,<span class="string">"value03"</span>));</span><br><span class="line">    System.out.println(<span class="string">"获取多个键值对："</span>+jedis.mget(<span class="string">"key01"</span>,<span class="string">"key02"</span>,<span class="string">"key03"</span>));</span><br><span class="line">    System.out.println(<span class="string">"获取多个键值对："</span>+jedis.mget(<span class="string">"key01"</span>,<span class="string">"key02"</span>,<span class="string">"key03"</span>,<span class="string">"key04"</span>));</span><br><span class="line">    System.out.println(<span class="string">"删除多个键值对："</span>+jedis.del(<span class="keyword">new</span> String[]&#123;<span class="string">"key01"</span>,<span class="string">"key02"</span>&#125;));</span><br><span class="line">    System.out.println(<span class="string">"获取多个键值对："</span>+jedis.mget(<span class="string">"key01"</span>,<span class="string">"key02"</span>,<span class="string">"key03"</span>));</span><br><span class="line"></span><br><span class="line">    jedis.flushDB();</span><br><span class="line">    System.out.println(<span class="string">"===========新增键值对防止覆盖原先值=============="</span>);</span><br><span class="line">    System.out.println(jedis.setnx(<span class="string">"key1"</span>, <span class="string">"value1"</span>));</span><br><span class="line">    System.out.println(jedis.setnx(<span class="string">"key2"</span>, <span class="string">"value2"</span>));</span><br><span class="line">    System.out.println(jedis.setnx(<span class="string">"key2"</span>, <span class="string">"value2-new"</span>));</span><br><span class="line">    System.out.println(jedis.get(<span class="string">"key1"</span>));</span><br><span class="line">    System.out.println(jedis.get(<span class="string">"key2"</span>));</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="string">"===========新增键值对并设置有效时间============="</span>);</span><br><span class="line">    System.out.println(jedis.setex(<span class="string">"key3"</span>, <span class="number">2</span>, <span class="string">"value3"</span>));</span><br><span class="line">    System.out.println(jedis.get(<span class="string">"key3"</span>));</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        TimeUnit.SECONDS.sleep(<span class="number">3</span>);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    System.out.println(jedis.get(<span class="string">"key3"</span>));</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="string">"===========获取原值，更新为新值=========="</span>);</span><br><span class="line">    System.out.println(jedis.getSet(<span class="string">"key2"</span>, <span class="string">"key2GetSet"</span>));</span><br><span class="line">    System.out.println(jedis.get(<span class="string">"key2"</span>));</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="string">"获得key2的值的字串："</span>+jedis.getrange(<span class="string">"key2"</span>, <span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>对List的操作</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testList</span><span class="params">()</span></span>&#123;</span><br><span class="line">    jedis.flushDB();</span><br><span class="line">    System.out.println(<span class="string">"===========添加一个list==========="</span>);</span><br><span class="line">    <span class="comment">//jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");</span></span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>, <span class="string">"HashSet"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>, <span class="string">"TreeSet"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>, <span class="string">"TreeMap"</span>);</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));<span class="comment">//-1代表倒数第一个元素，-2代表倒数第二个元素,end为-1表示查询全部</span></span><br><span class="line">    System.out.println(<span class="string">"collections区间0-3的元素："</span>+jedis.lrange(<span class="string">"collections"</span>,<span class="number">0</span>,<span class="number">3</span>));</span><br><span class="line">    System.out.println(<span class="string">"==============================="</span>);</span><br><span class="line">    <span class="comment">// 删除列表指定的值 ，第二个参数为删除的个数（有重复时），后add进去的值先被删，类似于出栈</span></span><br><span class="line">    System.out.println(<span class="string">"删除指定元素个数："</span>+jedis.lrem(<span class="string">"collections"</span>, <span class="number">2</span>, <span class="string">"HashMap"</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(<span class="string">"删除下表0-3区间之外的元素："</span>+jedis.ltrim(<span class="string">"collections"</span>, <span class="number">0</span>, <span class="number">3</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections列表出栈（左端）："</span>+jedis.lpop(<span class="string">"collections"</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections添加元素，从列表右端，与lpush相对应："</span>+jedis.rpush(<span class="string">"collections"</span>, <span class="string">"EnumMap"</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections列表出栈（右端）："</span>+jedis.rpop(<span class="string">"collections"</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(<span class="string">"修改collections指定下标1的内容："</span>+jedis.lset(<span class="string">"collections"</span>, <span class="number">1</span>, <span class="string">"LinkedArrayList"</span>));</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(<span class="string">"==============================="</span>);</span><br><span class="line">    System.out.println(<span class="string">"collections的长度："</span>+jedis.llen(<span class="string">"collections"</span>));</span><br><span class="line">    System.out.println(<span class="string">"获取collections下标为2的元素："</span>+jedis.lindex(<span class="string">"collections"</span>, <span class="number">2</span>));</span><br><span class="line">    System.out.println(<span class="string">"==============================="</span>);</span><br><span class="line">    <span class="comment">//jedis.lpush("sortedList", "3","6","2","0","7","4");</span></span><br><span class="line">    System.out.println(<span class="string">"sortedList排序前："</span>+jedis.lrange(<span class="string">"sortedList"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(jedis.sort(<span class="string">"sortedList"</span>));</span><br><span class="line">    System.out.println(<span class="string">"sortedList排序后："</span>+jedis.lrange(<span class="string">"sortedList"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>对Set的操作</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSet</span><span class="params">()</span></span>&#123;</span><br><span class="line">    jedis.flushDB();</span><br><span class="line">    System.out.println(<span class="string">"============向集合中添加元素（不重复）============"</span>);</span><br><span class="line">    System.out.println(jedis.sadd(<span class="string">"eleSet"</span>, <span class="string">"e1"</span>,<span class="string">"e2"</span>,<span class="string">"e4"</span>,<span class="string">"e3"</span>,<span class="string">"e0"</span>,<span class="string">"e8"</span>,<span class="string">"e7"</span>,<span class="string">"e5"</span>));</span><br><span class="line">    System.out.println(jedis.sadd(<span class="string">"eleSet"</span>, <span class="string">"e6"</span>));</span><br><span class="line">    System.out.println(jedis.sadd(<span class="string">"eleSet"</span>, <span class="string">"e6"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet的所有元素为："</span>+jedis.smembers(<span class="string">"eleSet"</span>));</span><br><span class="line">    System.out.println(<span class="string">"删除一个元素e0："</span>+jedis.srem(<span class="string">"eleSet"</span>, <span class="string">"e0"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet的所有元素为："</span>+jedis.smembers(<span class="string">"eleSet"</span>));</span><br><span class="line">    System.out.println(<span class="string">"删除两个元素e7和e6："</span>+jedis.srem(<span class="string">"eleSet"</span>, <span class="string">"e7"</span>,<span class="string">"e6"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet的所有元素为："</span>+jedis.smembers(<span class="string">"eleSet"</span>));</span><br><span class="line">    System.out.println(<span class="string">"随机的移除集合中的一个元素："</span>+jedis.spop(<span class="string">"eleSet"</span>));</span><br><span class="line">    System.out.println(<span class="string">"随机的移除集合中的一个元素："</span>+jedis.spop(<span class="string">"eleSet"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet的所有元素为："</span>+jedis.smembers(<span class="string">"eleSet"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet中包含元素的个数："</span>+jedis.scard(<span class="string">"eleSet"</span>));</span><br><span class="line">    System.out.println(<span class="string">"e3是否在eleSet中："</span>+jedis.sismember(<span class="string">"eleSet"</span>, <span class="string">"e3"</span>));</span><br><span class="line">    System.out.println(<span class="string">"e1是否在eleSet中："</span>+jedis.sismember(<span class="string">"eleSet"</span>, <span class="string">"e1"</span>));</span><br><span class="line">    System.out.println(<span class="string">"e1是否在eleSet中："</span>+jedis.sismember(<span class="string">"eleSet"</span>, <span class="string">"e5"</span>));</span><br><span class="line">    System.out.println(<span class="string">"================================="</span>);</span><br><span class="line">    System.out.println(jedis.sadd(<span class="string">"eleSet1"</span>, <span class="string">"e1"</span>,<span class="string">"e2"</span>,<span class="string">"e4"</span>,<span class="string">"e3"</span>,<span class="string">"e0"</span>,<span class="string">"e8"</span>,<span class="string">"e7"</span>,<span class="string">"e5"</span>));</span><br><span class="line">    System.out.println(jedis.sadd(<span class="string">"eleSet2"</span>, <span class="string">"e1"</span>,<span class="string">"e2"</span>,<span class="string">"e4"</span>,<span class="string">"e3"</span>,<span class="string">"e0"</span>,<span class="string">"e8"</span>));</span><br><span class="line">    System.out.println(<span class="string">"将eleSet1中删除e1并存入eleSet3中："</span>+jedis.smove(<span class="string">"eleSet1"</span>, <span class="string">"eleSet3"</span>, <span class="string">"e1"</span>));<span class="comment">//移到集合元素</span></span><br><span class="line">    System.out.println(<span class="string">"将eleSet1中删除e2并存入eleSet3中："</span>+jedis.smove(<span class="string">"eleSet1"</span>, <span class="string">"eleSet3"</span>, <span class="string">"e2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet1中的元素："</span>+jedis.smembers(<span class="string">"eleSet1"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet3中的元素："</span>+jedis.smembers(<span class="string">"eleSet3"</span>));</span><br><span class="line">    System.out.println(<span class="string">"============集合运算================="</span>);</span><br><span class="line">    System.out.println(<span class="string">"eleSet1中的元素："</span>+jedis.smembers(<span class="string">"eleSet1"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet2中的元素："</span>+jedis.smembers(<span class="string">"eleSet2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet1和eleSet2的交集:"</span>+jedis.sinter(<span class="string">"eleSet1"</span>,<span class="string">"eleSet2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet1和eleSet2的并集:"</span>+jedis.sunion(<span class="string">"eleSet1"</span>,<span class="string">"eleSet2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"eleSet1和eleSet2的差集:"</span>+jedis.sdiff(<span class="string">"eleSet1"</span>,<span class="string">"eleSet2"</span>));<span class="comment">//eleSet1中有，eleSet2中没有</span></span><br><span class="line">    jedis.sinterstore(<span class="string">"eleSet4"</span>,<span class="string">"eleSet1"</span>,<span class="string">"eleSet2"</span>);<span class="comment">//求交集并将交集保存到dstkey的集合</span></span><br><span class="line">    System.out.println(<span class="string">"eleSet4中的元素："</span>+jedis.smembers(<span class="string">"eleSet4"</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="5">
<li>对Hash的操作</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testHash</span><span class="params">()</span></span>&#123;</span><br><span class="line">    jedis.flushDB();</span><br><span class="line">    Map&lt;String,String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    map.put(<span class="string">"key1"</span>,<span class="string">"value1"</span>);</span><br><span class="line">    map.put(<span class="string">"key2"</span>,<span class="string">"value2"</span>);</span><br><span class="line">    map.put(<span class="string">"key3"</span>,<span class="string">"value3"</span>);</span><br><span class="line">    map.put(<span class="string">"key4"</span>,<span class="string">"value4"</span>);</span><br><span class="line">    <span class="comment">//添加名称为hash（key）的hash元素</span></span><br><span class="line">    jedis.hmset(<span class="string">"hash"</span>,map);</span><br><span class="line">    <span class="comment">//向名称为hash的hash中添加key为key5，value为value5元素</span></span><br><span class="line">    jedis.hset(<span class="string">"hash"</span>, <span class="string">"key5"</span>, <span class="string">"value5"</span>);</span><br><span class="line">    System.out.println(<span class="string">"散列hash的所有键值对为："</span>+jedis.hgetAll(<span class="string">"hash"</span>));<span class="comment">//return Map&lt;String,String&gt;</span></span><br><span class="line">    System.out.println(<span class="string">"散列hash的所有键为："</span>+jedis.hkeys(<span class="string">"hash"</span>));<span class="comment">//return Set&lt;String&gt;</span></span><br><span class="line">    System.out.println(<span class="string">"散列hash的所有值为："</span>+jedis.hvals(<span class="string">"hash"</span>));<span class="comment">//return List&lt;String&gt;</span></span><br><span class="line">    System.out.println(<span class="string">"将key6保存的值加上一个整数，如果key6不存在则添加key6："</span>+jedis.hincrBy(<span class="string">"hash"</span>, <span class="string">"key6"</span>, <span class="number">6</span>));</span><br><span class="line">    System.out.println(<span class="string">"散列hash的所有键值对为："</span>+jedis.hgetAll(<span class="string">"hash"</span>));</span><br><span class="line">    System.out.println(<span class="string">"将key6保存的值加上一个整数，如果key6不存在则添加key6："</span>+jedis.hincrBy(<span class="string">"hash"</span>, <span class="string">"key6"</span>, <span class="number">3</span>));</span><br><span class="line">    System.out.println(<span class="string">"散列hash的所有键值对为："</span>+jedis.hgetAll(<span class="string">"hash"</span>));</span><br><span class="line">    System.out.println(<span class="string">"删除一个或者多个键值对："</span>+jedis.hdel(<span class="string">"hash"</span>, <span class="string">"key2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"散列hash的所有键值对为："</span>+jedis.hgetAll(<span class="string">"hash"</span>));</span><br><span class="line">    System.out.println(<span class="string">"散列hash中键值对的个数："</span>+jedis.hlen(<span class="string">"hash"</span>));</span><br><span class="line">    System.out.println(<span class="string">"判断hash中是否存在key2："</span>+jedis.hexists(<span class="string">"hash"</span>,<span class="string">"key2"</span>));</span><br><span class="line">    System.out.println(<span class="string">"判断hash中是否存在key3："</span>+jedis.hexists(<span class="string">"hash"</span>,<span class="string">"key3"</span>));</span><br><span class="line">    System.out.println(<span class="string">"获取hash中的值："</span>+jedis.hmget(<span class="string">"hash"</span>,<span class="string">"key3"</span>));</span><br><span class="line">    System.out.println(<span class="string">"获取hash中的值："</span>+jedis.hmget(<span class="string">"hash"</span>,<span class="string">"key3"</span>,<span class="string">"key4"</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="6">
<li>对排序的操作</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSort</span><span class="params">()</span></span>&#123;</span><br><span class="line">    jedis.flushDB();</span><br><span class="line">    <span class="comment">//jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");</span></span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>,<span class="string">"ArrayList"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>,<span class="string">"Vector"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>,<span class="string">"Stack"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>,<span class="string">"HashMap"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>,<span class="string">"WeakHashMap"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"collections"</span>,<span class="string">"LinkedHashMap"</span>);</span><br><span class="line">    System.out.println(<span class="string">"collections的内容："</span>+jedis.lrange(<span class="string">"collections"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    SortingParams sortingParameters = <span class="keyword">new</span> SortingParams();</span><br><span class="line">    System.out.println(jedis.sort(<span class="string">"collections"</span>,sortingParameters.alpha()));</span><br><span class="line">    System.out.println(<span class="string">"==============================="</span>);</span><br><span class="line">    <span class="comment">//jedis.lpush("sortedList", "3","6","2","0","7","4");</span></span><br><span class="line">    jedis.lpush(<span class="string">"sortedList"</span>,<span class="string">"3"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"sortedList"</span>,<span class="string">"6"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"sortedList"</span>,<span class="string">"2"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"sortedList"</span>,<span class="string">"0"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"sortedList"</span>,<span class="string">"7"</span>);</span><br><span class="line">    jedis.lpush(<span class="string">"sortedList"</span>,<span class="string">"4"</span>);</span><br><span class="line">    System.out.println(<span class="string">"sortedList排序前："</span>+jedis.lrange(<span class="string">"sortedList"</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    System.out.println(<span class="string">"升序："</span>+jedis.sort(<span class="string">"sortedList"</span>, sortingParameters.asc()));</span><br><span class="line">    System.out.println(<span class="string">"降序："</span>+jedis.sort(<span class="string">"sortedList"</span>, sortingParameters.desc()));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="2-3-操作事务"><a href="#2-3-操作事务" class="headerlink" title="2.3 操作事务"></a>2.3 操作事务</h3><ol>
<li>正常执行</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zhuuu;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSONObject;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.Transaction;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestTransication</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1. new Jedis对象</span></span><br><span class="line">        Jedis jedis = <span class="keyword">new</span> Jedis(<span class="string">"你的ip地址"</span>,<span class="number">6379</span>);</span><br><span class="line">        jedis.auth(<span class="string">"123456"</span>);</span><br><span class="line"></span><br><span class="line">        JSONObject jsonObject = <span class="keyword">new</span> JSONObject();</span><br><span class="line">        jsonObject.put(<span class="string">"hello"</span>,<span class="string">"world"</span>);</span><br><span class="line">        jsonObject.put(<span class="string">"name"</span>,<span class="string">"zhuuu"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 开启事务</span></span><br><span class="line">        Transaction multi = jedis.multi();</span><br><span class="line">        String result = jsonObject.toJSONString();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            multi.set(<span class="string">"user1"</span>,result);</span><br><span class="line">            multi.set(<span class="string">"user2"</span>,result);</span><br><span class="line"></span><br><span class="line">            multi.exec();     <span class="comment">// 执行事务</span></span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            multi.discard();  <span class="comment">// 放弃事务</span></span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="comment">// 关闭连接</span></span><br><span class="line">            System.out.println(jedis.get(<span class="string">"user1"</span>));</span><br><span class="line">            System.out.println(jedis.get(<span class="string">"user2"</span>));</span><br><span class="line">            jedis.close();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>事务失败</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zhuuu;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSONObject;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.Transaction;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestTransication</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1. new Jedis对象</span></span><br><span class="line">        Jedis jedis = <span class="keyword">new</span> Jedis(<span class="string">"你的ip地址"</span>,<span class="number">6379</span>);</span><br><span class="line">        jedis.auth(<span class="string">"123456"</span>);</span><br><span class="line"></span><br><span class="line">        jedis.flushDB();</span><br><span class="line">        </span><br><span class="line">        JSONObject jsonObject = <span class="keyword">new</span> JSONObject();</span><br><span class="line">        jsonObject.put(<span class="string">"hello"</span>,<span class="string">"world"</span>);</span><br><span class="line">        jsonObject.put(<span class="string">"name"</span>,<span class="string">"zhuuu"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 开启事务</span></span><br><span class="line">        Transaction multi = jedis.multi();</span><br><span class="line">        String result = jsonObject.toJSONString();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            multi.set(<span class="string">"user1"</span>,result);</span><br><span class="line">            multi.set(<span class="string">"user2"</span>,result);</span><br><span class="line">            <span class="keyword">int</span> i = <span class="number">1</span>/<span class="number">0</span>;      <span class="comment">// 代码抛出异常，执行失败</span></span><br><span class="line">            multi.exec();     <span class="comment">// 执行事务</span></span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            multi.discard();  <span class="comment">// 放弃事务</span></span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="comment">// 关闭连接</span></span><br><span class="line">            System.out.println(jedis.get(<span class="string">"user1"</span>));</span><br><span class="line">            System.out.println(jedis.get(<span class="string">"user2"</span>));</span><br><span class="line">            jedis.close();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">java.lang.ArithmeticException: / by zero</span><br><span class="line">	at com.zhuuu.TestTransication.main(TestTransication.java:<span class="number">26</span>)</span><br><span class="line"><span class="keyword">null</span></span><br><span class="line"><span class="keyword">null</span></span><br><span class="line"></span><br><span class="line">Process finished with exit code <span class="number">0</span></span><br></pre></td></tr></table></figure>


      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://zhuuu.work/2020/04/12/Redis/Redis-06-JRedis/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Redis/" rel="tag">Redis</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="tag">数据库</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/04/14/Redis/Redis-08-Conf/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Redis-08-Conf
          
        </div>
      </a>
    
    
      <a href="/2020/04/12/Redis/Redis-07-SpringBoot%E6%95%B4%E5%90%88/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Redis-07-SpringBoot整合</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
<div class="gitalk" id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1.5.0/dist/gitalk.css">


<script src="https://cdn.jsdelivr.net/npm/gitalk@1.5.0/dist/gitalk.min.js"></script>


<script src="https://cdn.jsdelivr.net/npm/blueimp-md5@2.10.0/js/md5.min.js"></script>

<script type="text/javascript">
  var gitalk = new Gitalk({
    clientID: 'db188ed8c86dc4b0dbf3',
    clientSecret: 'a58f92160e5a9efd726b7d533000a0737f3e3f3e',
    repo: 'Blog-comments',
    owner: 'Zhuuuuuuuu',
    admin: ['Zhuuuuuuuu'],
    // id: location.pathname,      // Ensure uniqueness and length less than 50
    id: md5(location.pathname),
    distractionFreeMode: false,  // Facebook-like distraction free mode
    pagerDirection: 'last'
  })

  gitalk.render('gitalk-container')
</script>

  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2019-2021
        Zhuuu
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="朱酱酱的学习博客"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/JVM/">JVM</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/JDK%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/">JDK源码</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/">多线程</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Mysql/">Mysql</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Redis/">Redis</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E8%AE%BE%E8%AE%A1%E8%80%85%E6%A8%A1%E5%BC%8F/">设计模式</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/MyBatis/">MyBatis</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/SpringMVC/">SpringMVC</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Spring/">Spring</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/SpringBoot/">SpringBoot</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">Linux</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Leetcode/">Leetcode</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E5%89%8D%E7%AB%AF/">前端</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/photoshop/">photoshop</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="http://smartzhuuu.lofter.com/" target="_blank" rel="noopener">摄影</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2020/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['昨夜西风凋碧树。独上高楼，望尽天涯路','衣带渐宽终不悔，为伊消得人憔悴。','众里寻他千百度。蓦然回首，那人却在，灯火阑珊处。'],
    startDelay: 0,
    typeSpeed: 200,
    loop: true,
    backSpeed: 100,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>